<?php

class Message {

	private $id;
	private $uuid;
	private $created_at;
	private $updated_at;

	public $to_domain;
	public $to_endpoint;
	public $from_domain;
	public $from_endpoint;
	public $raw_message;

	function __construct($id) {
		$this->id = $id;
	}	

	public function getID() {
		return $this->id;
	}

	public function getUUID() {
		return $this->uuid;
	}

	public function messages($domain, $endpoint, $status, $current_user) {
	
		$current_user = "test@test.com";
	
		// If current user is logged in split out address
		if($current_user != "") {
		
			// Separate domain and endpoint;
			$address = split("@",$current_user);
			$user_endpoint = $address[0];
			$user_domain = $address[1];
	
		}
		
		
		// Default to NEW messages
		$status = ($status != "") ? $status : "NEW";

		// Database Connection
		$db_connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
		
		$q = "SELECT * FROM messages, statuses 
			WHERE statuses.message_id = messages.message_id 
				AND message_to_domain = '$domain'
				AND message_to_endpoint = '$endpoint' 
				AND statuses.status_status = '$status'
				AND ((message_to_domain = '$user_domain' and message_to_endpoint = '$user_endpoint' ) 
					OR (message_from_domain = '$user_domain' and message_from_endpoint = '$user_endpoint'))";
		
				
		
		//$rs = mysqli_query($db_connection, $q);
		//$stmt = mysqli_prepare($db_connection, $q);
		//mysqli_stmt_bind_param($stmt, "sssssss", $domain, $endpoint, $status, $user_domain, $user_endpoint, $user_domain, $user_endpoint);
		//mysqli_stmt_execute($stmt);
		
		//(MYSQLI_STMT::result_metadata());
		
		$messages = array();
		$tempMessages = array();
		
		if($result = mysqli_query($db_connection, $q)) {
			while($row = mysqli_fetch_assoc($result)) {
				array_push($tempMessages, $row);				
			}
		}	
	
		foreach($tempMessages as $row) {
			$message = new Message($row['message_id']);
			$message->uuid = $row['message_uuid'];
			$message->to_domain = $row['message_to_domain'];
			$message->to_endpoint = $row['message_to_domain'];
			$message->from_domain = $row['message_from_domain'];
			$message->from_endpoint = $row['message_from_endpoint'];
			$message->updated_at = $row['message_updated_at'];
			$message->created_at = $row['message_created_at'];			
			$message->raw_message = $row['message_raw_message'];

			array_push($messages, $message);
			
		}	

		return $messages;
	}
	
	
	// Only return if this is the owner of the message
	public function get_message($uuid) {
		
		$db_connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
		
		$result = mysqli_query($db_connection, "SELECT * FROM messages WHERE message_uuid = $uuid LIMIT 0,1");
		$message = mysqli_fetch_assoc($result);
			
		$testDomain = new Domain($domain['domain_id']);
		$testDomain->created_at = $domain['domain_created_at'];
		$testDomain->domain = $domain['domain_domain'];
		$testDomain->updated_at = $domain['domain_updated_at'];
		$testDomain->local = $domain['domain_local'];

		return $testDomain;
		
	}
	
	
	public function new_domain($domain, $local) {
	
		$db_connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
		
		$stmt = mysqli_prepare($db_connection, "INSERT INTO domains (domain_domain, domain_local) VALUES (?, ?)");
		mysqli_stmt_bind_param($stmt, "si", $domain, $local);
		mysqli_stmt_execute($stmt);
	
		// Retrieve new record
		$id = $stmt->insert_id;
		
		$domain = Domain::get_domain($id);
		
		mysqli_stmt_close($stmt);
		
		return $domain;
	
	}
	
	
	public function update_domain($id, $domain, $local) {
	
		$db_connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
		
		$stmt = mysqli_prepare($db_connection, "UPDATE domains SET domain_domain = ?, domain_local = ? WHERE domain_id = ?");
		mysqli_stmt_bind_param($stmt, "sii", $domain, $local, $id);
		mysqli_stmt_execute($stmt);
	
		// Retrieve new record
		$id = $stmt->insert_id;
		
		$domain = Domain::get_domain($id);
		
		mysqli_stmt_close($stmt);
		
		return $domain;
	
	}
	
	
	public function remove_domain($id) {
	
		$db_connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);

		mysqli_query($db_connection, "DELETE FROM domains WHERE domain_id = $id");		
		//printf("%d Row deleted.\n", $mysqli->affected_rows);
	
	}

}

?>
